﻿using AutoMapper;
using B.S.Database.Domain.Entity;
using B.S.Database.ErrorCode;
using B.S.Database.Interface.Interface;
using B.S.Database.Write.Api.Application.Commands.Image;
using MediatR;

namespace B.S.Database.Write.Api.Application.CommandHandler.Image
{
    public class PictrueUpdatethreeCommandHandler : IRequestHandler<PictrueUpdatethreeCommands, Apiresult<object>>
    {
        private readonly IBaseResotity<Picture_Information> pictureRepo;
        private readonly IMapper mapper;

        public PictrueUpdatethreeCommandHandler(IBaseResotity<Picture_Information> PictureRepo, IMapper mapper)
        {
            pictureRepo = PictureRepo;
            this.mapper = mapper;
        }
        public Task<Apiresult<object>> Handle(PictrueUpdatethreeCommands request, CancellationToken cancellationToken)
        {
            Apiresult<object> res = new Apiresult<object>();
            var existingPicture = pictureRepo.GetAll().FirstOrDefault(x => x.TraceabilityId == request.TraceabilityId);
            // 如果存在，则更新现有记录
            existingPicture.Certification_Documents = null;
            pictureRepo.Update(existingPicture);
            res.Code = ApiEnums.成功;
            res.Message = "图片信息更新成功";
            return Task.FromResult(res);
        }
    }
}
